package com.sea.physical.service.impl;

import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.util.DateUtils;
import com.sea.data.entity.OrderDetail;
import com.sea.data.service.OrderDetailService;
import com.sea.physical.entity.*;
import com.sea.physical.mapper.CreateDespatchSkuMapper;
import com.sea.physical.service.CreateDespatchPlanService;
import com.sea.physical.service.CreateDespatchSkuService;
import com.sea.physical.service.CreatePlanService;
import com.sea.physical.service.CreateSkuService;
import com.sea.product.entity.Product;
import com.sea.product.service.ProductService;
import com.sea.supply.entity.Buy;
import com.sea.supply.entity.ProduceDetail;
import com.sea.supply.entity.Receive;
import com.sea.supply.entity.Supplier;
import com.sea.supply.service.BuyService;
import com.sea.supply.service.ProduceDetailService;
import com.sea.supply.service.ReceiveService;
import com.sea.supply.service.SupplierService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class CreateDespatchSkuServiceImpl implements CreateDespatchSkuService {

    @Autowired
    private CreateDespatchSkuMapper createDespatchSkuMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private CreateSkuService createSkuService;
    @Autowired
    private DictDetailService dictDetailService;
    @Autowired
    private ReceiveService receiveService;
    @Autowired
    private CreateDespatchPlanService createDespatchPlanService;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private CreatePlanService createPlanService;
    @Autowired
    private ProduceDetailService produceDetailService;

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private BuyService buyService;

    @Override
    public int save(CreateDespatchSku createDespatchSku) {
        return createDespatchSkuMapper.insert(createDespatchSku);
    }

    @Override
    public List<CreateDespatchSku> findByCreateId(Integer createId) {
        return createDespatchSkuMapper.findByCreateId(createId);
    }

    @Override
    public int usave(CreateDespatchSku createDespatchSku) {
        return createDespatchSkuMapper.update(createDespatchSku);
    }

    @Override
    public CreateDespatchSku findById(Integer id) {
        return createDespatchSkuMapper.findById(id);
    }

    @Override
    public List<CreateDespatchSku> getRows(Integer[] idList) {
        return createDespatchSkuMapper.getRows(idList);
    }

    @Override
    public CreateDespatchSku findByMonthChannelSku(String month, String channelName, String createSku) {
        Integer rateId=1;
        switch (channelName) {
            case "amazon.泛欧":
                rateId=4;
                break;
        }
        System.out.println(channelName+"--"+createSku);
        return createDespatchSkuMapper.findByMonthChannelSku(month, channelName, createSku,rateId);
    }

    @Override
    public int delete(Integer createId) {
        return createDespatchSkuMapper.delete(createId);
    }

    @Override
    public void getCreateSkuInfo(CreateDespatchSku createDespatchSku) {
        Integer createSkuId = createDespatchSku.getCreateSkuId();
        CreateSku createSku = createSkuService.findById(createSkuId);
        Product product = productService.findBySku(createSku.getCreateSku());
        Integer createNumber = createDespatchSku.getCreateNumber();
        createDespatchSku.setCreateNumberTotal(createNumber*product.getProductContainCount());
        createDespatchSku.setCreateBoxNumber(((double)createNumber/(double)createSku.getCreatePackage()));
        createDespatchSku.setCreateWeightTotal(createDespatchSku.getCreateBoxNumber() * createSku.getCreateWeight());
        createDespatchSku.setCreateKgTotal(createNumber * product.getProductWeight());
        createDespatchSku.setCreateVolumeTotal(createDespatchSku.getCreateBoxNumber() * createSku.getCreateVolume());
        createDespatchSkuMapper.update(createDespatchSku);
    }

    @Override
    public List<CreateDespatchSku> findSalesPhysicalNumber(CreateDespatchSku createDespatchSku) {
        return createDespatchSkuMapper.findSalesPhysicalNumber(createDespatchSku);
    }

    @Override
    public List<CreateDespatchReport> getReport(CreateDespatchReport createDespatchReport) {
        return createDespatchSkuMapper.getReport(createDespatchReport);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void receive(Integer createId,String userRealname,String receiveDate) {
        //超收比例
        String receivePercent = dictDetailService.findByTypeCode(26, "1");
        if (StringUtils.isEmpty(receivePercent)) {
            throw new RuntimeException("无法查询到超售比例！");
        }
        double percent = Double.parseDouble(receivePercent);
        Integer produceId = 0;
        CreateDespatchPlan plan=new CreateDespatchPlan();
        List<Receive> list = getReceiveByCreateId(createId);
        for (Receive receive : list) {
            plan.setCreateSku(receive.getReceiveSku());
            plan.setCreatePlanNo(receive.getReceivePlanNo());
            List<CreateDespatchPlan> pList = createDespatchPlanService.findByCnd(plan);
            if(null==pList || pList.size()==0) {
                throw new RuntimeException("暂未查询到SKU信息！【" + receive.getReceiveSku() + "】");
            }
            Supplier supplier = supplierService.findBySupplierName(receive.getReceiveWare());
            if(null==supplier){
                throw new RuntimeException("暂未查询到供应商信息！【"+receive.getReceiveWare()+"】");
            }
            receive.setReceiveDate(receiveDate);
            if(supplier.getSupplierWare().equals("否")) {

                receiveService.receive(receive, produceId, userRealname, percent);
            }else{
                receiveService.wareReceive(receive,userRealname, produceId);
            }
        }
    }

    public List<Receive> getReceiveByCreateId(Integer createId){
        List<Receive> list=new ArrayList<>();
        CreatePlan createPlan = createPlanService.findById(createId);
        List<CreateDespatchSku> despatchSkuList = findByCreateId(createId);
        for(CreateDespatchSku createDespatchSku:despatchSkuList){
            Receive receive=new Receive();
            receive.setReceiveNo(createDespatchSku.getErpProducePo());

            CreateSku createSku = createSkuService.findById(createDespatchSku.getCreateSkuId());
            receive.setReceiveChannelName(createSku.getCreateChannelName());
            receive.setReceiveSku(createSku.getCreateSku());
            Integer createNumber = createDespatchSku.getCreateNumber();
            if(createNumber>0){
                receive.setReceiveNumber(createNumber);
                receive.setReceivePlanNo(createPlan.getCreatePlanNo());

                Supplier supplier = supplierService.findBySupplierName(createDespatchSku.getCreateSupplierName());
                if(null==supplier){
                    throw new RuntimeException("暂未查询到供应商信息！【"+receive.getReceiveWare()+"】");
                }
                receive.setReceiveWare(createDespatchSku.getCreateSupplierName());
                //找对应的交期
                if(supplier.getSupplierWare().equals("否")) {
                    ProduceDetail produceDetail = produceDetailService.findById(createDespatchSku.getCreateProduceId());

                    Buy buy = buyService.findById(produceDetail.getBuyId());
                    receive.setOrderDate(buy.getBuyOrderDate());
                }else{

                }
                list.add(receive);
            }


        }
        return list;
    }
}
